package com.spring.student.systemDB;

import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.Rowmapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Component("scoreManage")
public class ScoreManageImpl implements ScoreManage{
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void addScore(Score score){
        String sql = "insert into score(number,Chinese,math,English)value(?,?,?,?)";
        Object[]obj = new Object[]{
                score.getNumber(),
                score.getChinese(),
                score.getMath(),
                score.getEnglish()

        };
        int num = jdbcTemplate.update(sql,obj);
        if(num<=0)
            System.out.println("插入学生成绩记录失败");
    }

    @Override
    public void updataScore(String number, String course, Double score) {

    }

    @Override
    public void updateScore(String number,String course,Double score){
        String sql = "update scores set"+ course+"=? where number=?";
        Object[]params = new Object[]{score,number};
        int num =jdbcTemplate.update(sql,params);
        if(num<=0)
            System.out.println("更新学生成绩记录失败");
        }
        @Override
    public void queryScoreByNum(String number){
        String sql="select*from score where number=?";
        RowMapper<Score>rowMapper;
        rowMapper = new BeanPropertyRowMapper<Score.class>;
        Score score = jdbcTemplate.queryForObject(sql,rowMapper,number);
        System.out.println(score);
    }
    @Override
    public void queryAllScore(){
        String sql = "select*from scores";
        RowMapper<Score>rowMapper;
        rowMapper = new BeanPropertyRowMapper<Score>(Score.class);
        List<Score> score = jdbcTemplate.query(sql,rowMapper);
        for(Score score:scores){
            System.out.println(score);
        }

    }
}
